package GDoc::DB;

use strict;
use warnings;

use base 'DBIx::Class::Schema';

our $CONF;

BEGIN { 
    $CONF = do '/govdoc/etc/db.conf';
};


__PACKAGE__->load_classes;


# setup accessors for each table RS
# (maybe we can do this through MOP?)
foreach my $t( __PACKAGE__->sources ) { 
    no strict 'refs';

    *{ lc $t } = sub { 
        return shift->{schema}->resultset( $t );
    };

}


sub new { 
    my $class = shift;

    my $dsn = 'dbi:mysql:' . $CONF->{db};
    $dsn .= ';host=' . $CONF->{host};

    my $schema = $class->connection( $dsn, $CONF->{user}, $CONF->{password},
                                     { AutoCommit => 1 },
                                     { quote_char => q{`}, name_sep => q{.} }
                                   );

    return bless { schema => $schema };
}



1;
